package com.yuke.cloud.service.pmc.controller.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.*;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.pmc.dto.ProdSpecInfoAndPriceDto;
import com.yuke.cloud.service.pmc.dto.ProvinceAndCityInfoDto;
import com.yuke.cloud.service.pmc.entity.ProdSpec;
import com.yuke.cloud.service.pmc.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "WEB - PmcSpecFeignClient", tags = "处理ProdSpecApi相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping
public class PmcFeignClient implements PmcFeignApi {

    @Resource
    private ProdSpecService prodSpecService;
    @Resource
    private ProdCategoryService prodCategoryService;
    @Resource
    private DicRegionService dicRegionService;
    @Resource
    private ProdProductService prodProductService;
    @Resource
    private ProdFactoryService prodFactoryService;
    @Resource
    private ProdRegionIntroduceService prodRegionIntroduceService;
    @Resource
    private ProdStorageService prodStorageService;

    @Override
    @ApiOperation(httpMethod = "GET", value = "根据specId获取SpecInfo")
    // todo 使用该接口返回的价格时要注意是零售或批发价，目前默认为零售价
    public ProdSpecInfoDto getSpecInfoDtoBySpecId(Long specId) {
        return prodSpecService.getSpecInfoBySpecId(specId);
    }

    @Override
    public Wrapper<String> getSpecNameBySpecId(Long specId) {
        ProdSpec prodSpec = prodSpecService.selectByKey(specId);
        return WrapMapper.ok(prodSpec.getSpecName());
    }

    @Override
    public List<ProdSpecInfoDto> getSpecInfoDtosByCategoryId(Long categoryId) {
        return prodSpecService.getSpecInfoDtosByCategoryId(categoryId);
    }

    @Override
    public Wrapper<List<ProdSpecInfoDto>> getSpecInfoDtosByCondition(@RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        return WrapMapper.ok(prodSpecService.getSpecInfoDtosByCondition(prodSpecInfoDto));
    }

    @Override
    public List<ProdSpecInfoDto> getSpecInfoDtosBySpecIds(String specIds) {
        if (specIds.equals("")||specIds==null)
            return null;

        return prodSpecService.getSpecInfoListBySpecIds(Arrays.asList(specIds.split(",")).stream().map(s -> Long.valueOf(s)).collect(Collectors.toList()));
    }

    @Override
    public List<ProdSpecInfoDto> getSpecDtosBySpecIds(@RequestBody List<Long> specIds) {
        if (specIds.equals("")||specIds==null)
            return null;

        return prodSpecService.getSpecInfoListBySpecIds(specIds);
    }


    @Override
    @ApiOperation(httpMethod = "GET", value = "根据specId获取Category")
    public List<ProdCategoryDto> getCategorysBySpecId(Long specId) {
        return prodCategoryService.getCategorysBySpecId(specId);
    }

    @Override
    public List<ProdCategoryDto> getCategorysBySpecIds(@RequestBody List<Long> specIds) {
        return prodCategoryService.getCategorysBySpecIds(specIds);
    }

    @Override
    public JSONObject getPriceBySpecId(Long specId) {
        Map<String, Integer> prices = prodSpecService.getPriceBySpecId(specId);
        JSONObject pricesJson = JSONObject.parseObject(JSON.toJSONString(prices));
        return pricesJson;
    }

    @Override
    public Wrapper<String> getRegionNameByRegionId(Long regionId) {
        return WrapMapper.ok(dicRegionService.selectByKey(regionId).getRegionName());
    }

    @Override
    public List<ShopInfoDto> getRegionNameByRegionIds(@RequestBody List<ShopInfoDto> shopInfoDtos) {
        return dicRegionService.getRegionNameByRegionIds(shopInfoDtos);
    }

    @Override
    public RegionInfoDto getProvinceAndCityInfoByCityId(@RequestBody Long cityId) {
        ProvinceAndCityInfoDto getInfoByCityId = dicRegionService.getProvinceAndCityInfoByCityId(cityId);
        RegionInfoDto regionInfoDto = new RegionInfoDto();
        regionInfoDto.setRegionId(cityId);
        regionInfoDto.setRegionName(getInfoByCityId.getProvinceName()+getInfoByCityId.getCityName());
        return regionInfoDto;
    }

    //批量导入
    @Override
    public List<ProdSpecInfoDto> getImportSpecId(String specName) {
        List<ProdSpecInfoDto> improtSpecId = prodSpecService.getImportSpecId(specName);
        return improtSpecId;
    }

    @Override
    public List<ProdSpecInfoDto> getSpecBySpecName(@RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        return prodSpecService.getSpecBySpecName(prodSpecInfoDto);
    }

    @Override
    public List<ProdSpecInfoDto> getProdNameByProductId(@RequestBody List<Long> productIds) {
        if (productIds == null || productIds.size() < 1 ) {
            return null;
        }
        return prodProductService.getProdNameByProductId(productIds);
    }

    @Override
    public Wrapper updateSpecStockPrice(@RequestBody ProdSpecInfoDto prodSpecInfoDto) {
        log.info("------------"+prodSpecInfoDto);
        ProdSpec prodSpec = new ProdSpec();
        prodSpec.setSpecId(prodSpecInfoDto.getSpecId());
        prodSpec.setStockPrice(prodSpecInfoDto.getStockPrice());
        prodSpec.setUpdateId(prodSpecInfoDto.getUpdateUserId());
        return WrapMapper.ok(prodSpecService.updateSpecStockPrice(prodSpec));
    }

    @Override
    public Wrapper<List<RegionInfoDto>> getRegionIntroduceInfoByIds(@RequestBody List<Long> regionIds) {
        List<RegionInfoDto> regionInfoDtos = new ArrayList<>();
        if (regionIds == null || regionIds.size() == 0) {
            return WrapMapper.ok(regionInfoDtos);
        }
        regionInfoDtos = prodRegionIntroduceService.getRegionIntroduceInfoByIds(regionIds);
        return WrapMapper.ok(regionInfoDtos);
    }

    @Override
    public Wrapper<List<FactoryInfoDto>> getFactoryDetail(@RequestBody List<Long> factoryIds) {
        List<FactoryInfoDto> factoryInfoDtos = new ArrayList<>();
        if (factoryIds == null || factoryIds.size() == 0) {
            return WrapMapper.ok(factoryInfoDtos);
        }
        factoryInfoDtos = prodFactoryService.getFactoryDetail(factoryIds);
        return WrapMapper.ok(factoryInfoDtos);
    }

    @Override
    public Wrapper updateSpecSaleCount(@RequestBody List<ProdSpecInfoDto> prodSpecInfoDtos) {
        prodSpecService.updateSpecSaleCount(prodSpecInfoDtos);
        return WrapMapper.ok();
    }

    @Override
    public Wrapper<List<ProdSpecInfoExDto>> getSpecBySpecIds(@RequestParam Long shopId, @RequestBody List<Long> specIds, @RequestParam Byte saleType) {
        List<ProdSpecInfoExDto> prodSpecInfoExDtos = new ArrayList<>();
        List<ProdSpecInfoAndPriceDto> prodSpecInfoAndPriceDtos = prodSpecService.getSpecBySpecIds(shopId, specIds, saleType);

        for (ProdSpecInfoAndPriceDto prodSpecInfoAndPriceDto : prodSpecInfoAndPriceDtos) {
            ProdSpecInfoExDto prodSpecInfoExDto = new ProdSpecInfoExDto();
            BeanUtils.copyProperties(prodSpecInfoAndPriceDto, prodSpecInfoExDto);
            prodSpecInfoExDtos.add(prodSpecInfoExDto);
        }

        return WrapMapper.ok(prodSpecInfoExDtos);
    }

    @Override
    public List<ProdSpecInfoExDto> getSpecIds(@RequestBody ProdStorageDto prodStorageDto) {
        List<ProdSpecInfoExDto> specIds = prodSpecService.getSpecIds(prodStorageDto);
        return specIds;
    }

    @Override
    public List<ProdSpecInfoExDto> getAppSpecInfoBySpecIds(@RequestBody List<Long> specIds) {
        return prodStorageService.getAppSpecInfoBySpecIds(specIds);
    }

    /**
     * 获取商品主属性
     * @param specIds
     * @return
     */
    @Override
    public List<ProdAttributeDto> getFindProdAttribute(@RequestBody List<Long> specIds) {
        List<ProdAttributeDto> findProdAttributes = prodSpecService.getFindProdAttributes(specIds);
        return findProdAttributes;
    }

    @Override
    public List<ProdSpecInfoDto> getSpecInfoBySpecIds(@RequestBody  List<Long> specIds){
        return prodSpecService.getStorageSpecInfoBySpecIds(specIds);
    }

    @Override
    public List<Map> getRegionNameByCountyIds(@RequestBody List<Long> countys) {
        return dicRegionService.getRegionNameByCountyIds(countys);
    }

    @Override
    public List<ProdSpecInfoDto> getSpecIdByCategoryId(@RequestBody ProdCategoryDto prodCategoryDto) {
        return prodSpecService.getSpecIdByCategoryId(prodCategoryDto);
    }


    @Override
    public List<ProdSpecApiDto> getSpecNameBySpecIds(@RequestBody  List<Long> list) {
        List<ProdSpecApiDto> prodSpecApiDtoList = prodSpecService.getSpecNameBySpecIds(list);
        return prodSpecApiDtoList;
    }

    @Override
    public  List<Long> findLeaf(@RequestBody ProdCategoryDto prodCategoryDto) {
        return prodCategoryService.findLeaf(prodCategoryDto);
    }

    @Override
    public List<ProdSpecApiDto> findParent(@RequestBody List<Long> list) {
        return prodCategoryService.findParent(list);
    }

    @Override
    public List<ProdSpecApiDto> checkSpeCode(@RequestBody  List<String> specCode) {
        return prodSpecService.checkSpeCode(specCode);
    }


}
